package yxy.game.pm2.active;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;

import yxy.apple.util.FileUtility;
import yxy.apple.util.JSONUtility;
import yxy.apple.util.MD5Utility;
import yxy.apple.util.ReflectionUtility;
import yxy.cherry.data.bean.model.DataClass;

public class DataClassCheck {
	public static void main(String[] args) throws Exception {
		File[] files = FileUtility.getFiles(System.getProperty("user.dir") + "/data/", ".json");
		
//		Set<Class<? extends DataClass>> dataTypeSet = ReflectionUtility.getSubTypesOf("yxy.game.pm2.active.data.bean", DataClass.class);
		Set<Class<? extends DataClass>> dataTypeSet = ReflectionUtility.getSubTypesOf("yxy.cherry.data.bean", DataClass.class);
		Map<String, Class<? extends DataClass>> dataTypeMap = new HashMap<>();
		for (Class<? extends DataClass> clazz : dataTypeSet) {
			String name = clazz.getSimpleName();
			if(!name.contains("act")) {
				continue;
			}
			dataTypeMap.put(name, clazz);
		}

		// 逐个存储文件
		for (File file : files) {
			String filename = FileUtility.getFileNameWithoutExtension(file);// 文件名
			if (dataTypeMap.containsKey(filename)) {
				System.err.println("check file ::" + filename);
				Class<? extends DataClass> clazz = dataTypeMap.get(filename);
				JSONArray dataArray = JSONUtility.readArray(file);
				for (int i = 0; i < dataArray.size(); i++) {
					DataClass dc = dataArray.getObject(i, clazz);
//					System.out.println(dc.getCode());
				}
				continue;
			}

			System.err.println(filename + " is no class");
		}
	}

	static private DataClassCheck instance;

	public static DataClassCheck getInstance() {
		if (instance == null) {
			instance = new DataClassCheck();
		}
		return instance;
	}

	private HashMap<String, String> fileMD5 = new HashMap<>();

	public void inputFile(File file) {
		try {
			String filename = file.getName();
			String md5 = MD5Utility.getMD5(file);
			fileMD5.put(filename, md5);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void check() {
		for (Entry<String, String> entry : fileMD5.entrySet()) {
			System.err.println(entry.getKey() + "::" + entry.getValue());
		}
	}

}
